#include "sprite_ship_base.h"

SPRITE_SHIP_BASE::SPRITE_SHIP_BASE(CASH *cash, QGraphicsItem *parent = 0):QGraphicsObject(parent)
{
    this->cash = cash;
    index = 0;
    str_state = "shuttle";

    load();

    vect_pix = hash_sprites->value("shuttle_bounce");

    connect(this, SIGNAL(finished()), this, SLOT(slot_finished()));
}

SPRITE_SHIP_BASE::~SPRITE_SHIP_BASE()
{
}

void SPRITE_SHIP_BASE::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->save();
    painter->drawPixmap(0, 0, *vect_pix->at(index));
    painter->restore();
}

QRectF SPRITE_SHIP_BASE::boundingRect() const
{
    return QRectF(0, 0, vect_pix->at(index)->width(), vect_pix->at(index)->height());
}

QPainterPath SPRITE_SHIP_BASE::shape() const
{
    return QPainterPath();
}

void SPRITE_SHIP_BASE::timerEvent(QTimerEvent *)
{

    if(index > vect_pix->count() - 2)
    {
        this->stop();
        emit finished();
        return;
    }

    ++index;
    this->update();
}

void SPRITE_SHIP_BASE::load()
{
    hash_sprites = new QHash<QString, QVector<QPixmap*>*>();

    QDir dir("Res/Ships/Shuttle/sprites/");

    QStringList lst = dir.entryList();

    if(lst.isEmpty()){
        qDebug() << "ERROR!!! Shuttle sprites empty!!!";
        QApplication::closeAllWindows();
    }

    lst.removeOne("..");
    lst.removeOne(".");

    QVector<QPixmap*> *vect_tmp;

    foreach(QString s, lst){
        dir.setPath("Res/Ships/Shuttle/sprites/" + s + "/");
        vect_tmp = new QVector<QPixmap*>();

        for(int i=0;i<dir.entryList().count() - 3;i++){//- 3 исключая .. и .
            vect_tmp->append(cash->cash_pixmap->value("Res/Ships/Shuttle/sprites/" + s + "/" + QString::number(i) + ".png"));
        }

        hash_sprites->insert(s, vect_tmp);
    }
}

void SPRITE_SHIP_BASE::start()
{
    index = 0;
    timer_id = this->startTimer(30);
}

void SPRITE_SHIP_BASE::stop()
{
    this->killTimer(timer_id);
}

void SPRITE_SHIP_BASE::slot_finished()
{
    if(flag_bounce){
        vect_pix = hash_sprites->value(str_state + "_bounce");
        start();
    }
}

void SPRITE_SHIP_BASE::slot_fly(MOVE_DIRECTION move)
{
    switch (move) {
    case LEFT:
        stop();
        vect_pix = hash_sprites->value(str_state + "_left_in");
        flag_bounce = false;
        start();

        break;

    case RIGHT:
        stop();
        vect_pix = hash_sprites->value(str_state + "_right_in");
        flag_bounce = false;
        start();

        break;

    default:
        break;

    }
}

void SPRITE_SHIP_BASE::slot_fly_finish(MOVE_DIRECTION move)
{
    switch (move) {
    case LEFT:
        stop();
        vect_pix = hash_sprites->value(str_state + "_left_out");
        flag_bounce = true;
        start();

        break;

    case RIGHT:
        stop();
        vect_pix = hash_sprites->value(str_state + "_right_out");
        flag_bounce = true;
        start();

        break;

    default:
        break;

    }
}

void SPRITE_SHIP_BASE::slot_transform(QString old_state, QString new_state)
{
    if(new_state == str_state)
        return;

    stop();
    str_state = new_state;
    vect_pix = hash_sprites->value(old_state + "_to_" + new_state);
    flag_bounce = true;
    start();
}
